﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Web.UI;
using System.Web.UI.WebControls;
using AB.ServiceDirectory.BO;

namespace AB.ServiceDirectory.Design.WebUserControls.Frame
{
    public partial class RoleFrame : UserControl
    {
        /// <summary>
        /// Handles the Click event of the ButtonOk control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void ButtonOk_Click(object sender, EventArgs e)
        {
            string roles = GetRoles();
            var hiddenFieldSeriveId1 = Parent.FindControl("HiddenFieldServiceId1") as HiddenField;
            var hiddenFiledOrganizationId = Parent.FindControl("HiddenFieldOrganizationId") as HiddenField;

            int serviceId = 0;
            int organizationId = 0;
            if (hiddenFieldSeriveId1 != null)
                serviceId = Convert.ToInt32(hiddenFieldSeriveId1.Value, CultureInfo.CurrentCulture);
            if (hiddenFiledOrganizationId != null)
                organizationId = Convert.ToInt32(hiddenFiledOrganizationId.Value, CultureInfo.CurrentCulture);

            HServiceOrganizationBO.UpdateServiceOrganizationRole(serviceId, organizationId, roles);

            PanelContainer.Visible = false;

            var serviceOrganizationFrame = Parent as HServiceOrganizationFrame;
            if (serviceOrganizationFrame != null)
            {
                serviceOrganizationFrame.GridView_DataBind();
            }
        }

        /// <summary>
        /// Gets the roles from checkbox.
        /// join roles
        /// </summary>
        /// <returns></returns>
        private string GetRoles()
        {
            var roles = new List<string>();

            string result = string.Empty;

            if (CheckBoxFunding.Checked)
            {
                roles.Add(CheckBoxFunding.Text);
            }

            if (CheckBoxLead.Checked)
            {
                roles.Add(CheckBoxLead.Text);
            }

            if (CheckBoxDelivery.Checked)
            {
                roles.Add(CheckBoxDelivery.Text);
            }

            if (CheckBoxAuditor.Checked)
            {
                roles.Add(CheckBoxAuditor.Text);
            }

            if (roles.Count == 0)
            {
                result = string.Empty;
            }
            if (roles.Count == 1)
            {
                result = roles[0];
            }

            if (roles.Count >= 2)
            {
                foreach (string role in roles)
                {
                    result += role + ", ";
                }
            }

            result = result.TrimEnd(' '); //remove the last space of the string result
            result = result.TrimEnd(','); //remove the last comma of the string result

            return result;
        }

        /// <summary>
        /// Binds the roles to checkbox when RoleFrame is loaded.
        /// </summary>
        public void BindRoles()
        {
            var hiddenFieldRoles = (Parent.FindControl("HiddenFieldRoles") as HiddenField);
            if (hiddenFieldRoles.Value!=null)
            {
                string strRoles = hiddenFieldRoles.Value;
                strRoles = strRoles.Replace(" ", "");   //remove space in the string result
                string[] roles = strRoles.Split(',');
                for (int i = 0; i < roles.Length; i++)
                {
                    if (roles[i] == "Auditor")
                    {
                        CheckBoxAuditor.Checked = true;
                    }

                    if (roles[i] == "Delivery")
                    {
                        CheckBoxDelivery.Checked = true;
                    }

                    if (roles[i] == "Funder")
                    {
                        CheckBoxFunding.Checked = true;
                    }

                    if (roles[i]=="Lead")
                    {
                        CheckBoxLead.Checked = true;
                    }
                }

            }
        }

        /// <summary>
        /// Clears the check box.
        /// </summary>
        public void ClearCheckBox()
        {
            CheckBoxAuditor.Checked = false;
            CheckBoxFunding.Checked = false;
            CheckBoxDelivery.Checked = false;
            CheckBoxLead.Checked = false;
        }

        /// <summary>
        /// Handles the Click event of the ButtonCancel control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void ButtonCancel_Click(object sender, EventArgs e)
        {
            PanelContainer.Visible = false;
        }

        /// <summary>
        /// Popups this instance.
        /// </summary>
        public void Popup()
        {
            BindRoles();
            PanelContainer.CssClass = "popup_container";
            PanelContainer.Visible = true;
        }
    }
}